<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>调用记录管理</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://adminlte.io/themes/v3/dist/css/adminlte.min.css" rel="stylesheet">
</head>
<body>
    <nav class="main-header navbar navbar-expand navbar-white navbar-light">
        <a class="navbar-brand" href="index.php">后台管理</a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
            <span class="navbar-toggler-icon"></span>
        </button>
    </nav>

    <aside class="main-sidebar sidebar-dark-primary elevation-4">
        <div class="sidebar">
            <nav class="mt-2">
                <ul class="nav nav-pills nav-sidebar flex-column">
                    <li class="nav-item">
                        <a href="users.php" class="nav-link">用户管理</a>
                    </li>
                    <li class="nav-item">
                        <a href="tools.php" class="nav-link">小工具管理</a>
                    </li>
                    <li class="nav-item">
                        <a href="call-records.php" class="nav-link">调用记录管理</a>
                    </li>
                    <li class="nav-item">
                        <a href="logout.php" class="nav-link">登出</a>
                    </li>
                </ul>
            </nav>
        </div>
    </aside>

    <div class="content-wrapper">
        <section class="content-header">
            <div class="container-fluid">
                <div class="row mb-2">
                    <div class="col-sm-6">
                        <h1>调用记录管理</h1>
                    </div>
                </div>
            </div>
        </section>

        <section class="content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-header">
                                <h3 class="card-title">调用记录列表</h3>
                            </div>
                            <div class="card-body">
                                <table class="table table-bordered">
                                    <thead>
                                        <tr>
                                            <th>记录ID</th>
                                            <th>用户ID</th>
                                            <th>工具ID</th>
                                            <th>调用结果</th>
                                            <th>调用时间</th>
                                            <th>操作</th>
                                        </tr>
                                    </thead>
                                    <tbody id="callRecordsTableBody">
                                        <!-- 调用记录数据 -->
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function() {
            fetchCallRecords();
        });

        function fetchCallRecords() {
            $.ajax({
                url: 'get-call-records.php',
                method: 'GET',
                success: function(response) {
                    if (response.success) {
                        var callRecords = response.records;
                        var tableBody = $('#callRecordsTableBody');
                        tableBody.empty();
                        callRecords.forEach(function(record) {
                            var row = `
                                <tr>
                                    <td>${record.id}</td>
                                    <td>${record.user_id}</td>
                                    <td>${record.tool_id}</td>
                                    <td>${record.result}</td>
                                    <td>${record.created_at}</td>
                                    <td>
                                        <button type="button" class="btn btn-primary btn-sm" onclick="showCallResult(${record.id})">查看结果</button>
                                    </td>
                                </tr>
                            `;
                            tableBody.append(row);
                        });
                    } else {
                        $('#callRecordsTableBody').html('<tr><td colspan="6">暂无调用记录</td></tr>');
                    }
                },
                error: function(xhr, status, error) {
                    $('#callRecordsTableBody').html('<tr><td colspan="6">请求失败: ' + error + '</td></tr>');
                }
            });
        }

        function showCallResult(recordId) {
            $.ajax({
                url: 'get-call-result.php?id=' + recordId,
                method: 'GET',
                success: function(response) {
                    if (response.success) {
                        var resultHtml = '<div class="alert alert-success" role="alert">';
                        resultHtml += '<p>调用结果: ' + response.result + '</p>';
                        resultHtml += '</div>';
                        $('#result').html(resultHtml);
                        $('#callResultModal').modal('show');
                    } else {
                        $('#result').html('<div class="alert alert-danger" role="alert">' + response.message + '</div>');
                    }
                },
                error: function(xhr, status, error) {
                    $('#result').html('<div class="alert alert-danger" role="alert">请求失败: ' + error + '</div>');
                }
            });
        }
    </script>
</body>
</html>